package us.versus.them.kitkattoe;

import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.InterpolationMethod;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.SpreadMethod;
import flash.display.Sprite;

import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

import flash.geom.Matrix;

import flash.media.Sound;

import flash.text.TextField;
import flash.text.StyleSheet;

class Base extends Sprite {

	public static var w = flash.Lib.current.stage.stageWidth;
	public static var h = flash.Lib.current.stage.stageHeight;

	public static var DROP_FILTER =  [ new flash.filters.DropShadowFilter() ];
	public static var WHITE_FILTER = [ new flash.filters.DropShadowFilter(0,0,0xFFFFFF,1,5,5) ];
	public static var GLOW_FILTER =  [ new flash.filters.GlowFilter() ];

	public static var DEFAULT_STYLESHEET = _default_stylesheet();

	public function new() {
		super();
		//this.addEventListeners();
	}

	public function removeEventListeners() {
		this.removeEventListener( Event.ENTER_FRAME, enter_frame );
		this.removeEventListener( MouseEvent.MOUSE_DOWN, mouse_down );
		this.removeEventListener( MouseEvent.MOUSE_UP, mouse_up );
		this.removeEventListener( KeyboardEvent.KEY_DOWN , key_down );
	}

	public function addEventListeners() {
		this.addEventListener( Event.ENTER_FRAME, enter_frame );
		this.addEventListener( MouseEvent.MOUSE_DOWN, mouse_down );
		this.addEventListener( MouseEvent.MOUSE_UP, mouse_up );
		this.addEventListener( KeyboardEvent.KEY_DOWN , key_down );
	}

	public function mouse_down( mouseEvent : MouseEvent ) {
		//trace( mouseEvent );
	}

	public function mouse_up( mouseEvent : MouseEvent ) {
		//trace( mouseEvent );
	}

	public function key_down( keyboardEvent : KeyboardEvent ) {
		//trace( keyboardEvent );
	}

	public function enter_frame( _ ) {
		this.draw( this.graphics );
	}

	public function draw( g:Graphics );

	public function init() {
	}

	public static function displayObjectForName( name:String ) : DisplayObject {
		var cl = Type.resolveClass( name );
		return ( cast( Type.createInstance( cl, [] ), DisplayObject ) );
	}

	public static function soundForName( name:String ) : Sound {
		var cl = Type.resolveClass( name );
		var sound : Sound = null;
		try {
			// this just does not work... not sure why...
			sound = ( cast( Type.createInstance( cl, [] ), Sound ) );
		} catch ( e:Dynamic ) {
			trace( 'noSound 4u:' + e );
		}
		return sound;
	}

	public static function displayObjectToBitMapData( displayObject:DisplayObject ) {
		var bitmapData = new BitmapData( 
			  Math.floor( displayObject.width  )
			, Math.floor( displayObject.height )
			, true
			, 0xFF0000
		);
		bitmapData.draw( displayObject );

		return bitmapData;
	}

	public static function toRGB( r,g,b ) {
		return Math.floor( r ) << 16 | Math.floor( g ) << 8 | Math.floor( b );
	}

	////

	public static function text( tag, klass, text, x, y, styleSheet ) {
		var html = '<' + tag + ' class="' + klass + '">' + text + '</' + tag + '>';
		//var html = '<' + tag + '>' + text + '</' + tag + '>';

		var textField = Base.makeText( html, x, y, styleSheet );
		textField.filters = DROP_FILTER;

		return textField;
	}

	public static function makeText( html, x, y, styleSheet ) {
		var textField = new TextField();
		textField.autoSize = flash.text.TextFieldAutoSize.LEFT;
		textField.x = x;
		textField.y = y;
		textField.styleSheet = styleSheet;
		textField.htmlText = html;
		textField.alwaysShowSelection = false;
		textField.selectable = false;
		textField.wordWrap = false;
		return textField;
	}

	public static function f( n ) {
		return Math.floor( n );
	}

	public static function shuffle( ar:Array<Dynamic> ) {
		for ( i in 0 ... 2 * ar.length ) {
			var a = Math.floor( Math.random() * ar.length );
			var b = Math.floor( Math.random() * ar.length );
			var tmp = ar[ a ];
			ar[ a ] = ar[ b ];
			ar[ b ] = tmp;
		}
		return ar;
	}

	public static function randomElement( ar:Array<Dynamic> ) : Dynamic {
		return ar[ Math.floor( Math.random() * ar.length ) ];
	}

	private static function _default_stylesheet() {
		var styleSheet = new StyleSheet();
		styleSheet.parseCSS( ''
			+ 'h3 {'
			+ '    font-family: Arial, Helvetica, sans-serif;'
			+ '    font-size: 20px;'
			+ '    font-weight: 400;'
			+ '}'
			+ 'h4 {'
			+ '    font-family: Arial, Helvetica, sans-serif;'
			+ '    font-size: 24px;'
			+ '    font-weight: 600;'
			+ '}'
		);
		return styleSheet;
	}
}
